package com.fingard.dsp.bank.directbank.allinpay02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;

/**
 * 通联余额查询
 * 
 * @author Cruise
 * @date 2019/3/11
 */
public class AllInPay02Balance extends AllInPay02Base implements IBalance {
	@Override
	public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
		RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
		RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
		retAts.writeStart();

		try {
			AllInPay02Header bankHeader = new AllInPay02Header();
			bankHeader.REQ_SN = getQryTransID();
			bankHeader.TRX_CODE = "300000";

			while (p_req7506.readRecord()) {
				ActSetItem actItem = getBankConfig().getActSetItem(p_req7506.readedDetail.actNum, "unionRecPay");
				if (actItem == null) {
					tmpRetDetail.respCode = "0";
					tmpRetDetail.respInfo = "未找到账号" + p_req7506.readedDetail.actNum + "对应的商户配置信息";
					retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
					retAts.writeEnd();
					return retAts;
				}

				XmlTextWriter xmlWriter = new XmlTextWriter();
				writeAgrmCommonHeader(xmlWriter, bankHeader, actItem);
				xmlWriter.writeStartElement("ACQUERYREQ");
				xmlWriter.writeElementString("ACCTNO", actItem.inMerchantId);
				xmlWriter.writeEndDocument();

				String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), actItem);

				if (tmpStrRet[0].length() == 0) {
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

					if (tmpBkReader != null) {
						String tmpRespInfo = tmpBkReader.getFirstText("/AIPG/INFO/ERR_MSG");
						String tmpRespCode = tmpBkReader.getFirstText("/AIPG/INFO/RET_CODE");
						if ("0000".equals(tmpRespCode)) {
							if (tmpBkReader.read("/AIPG/ACQUERYREP/ACNODE")) {
								tmpRetDetail.respCode = "0";
								tmpRetDetail.respInfo = tmpRespInfo;
								tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
								tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
								tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
								tmpRetDetail.currBal = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("BALANCE"));
								tmpRetDetail.availBal = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("USABLEBAL"));
							}
						} else {
							tmpRetDetail.respCode = "-1";
							tmpRetDetail.respInfo = tmpRespInfo;
						}
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = tmpSbMsg.toString();
					}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpStrRet[1];
				}
			}
			retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.writeRespCode("-1");
			retAts.writeRespInfo(ex.getMessage());
			WriteBankLogLn(ex);
		}

		retAts.writeEnd();
		return retAts;
	}

	@Override
	public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
		RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
		String tmpRespCode = "";
		String tmpRespInfo = "";
		retAts.writeStart();
		
		try {
			Calendar tmpCalendarFrom = Calendar.getInstance();
			tmpCalendarFrom.setTime(p_req7505.getDateFrom());

			Calendar tmpCalendarTo = Calendar.getInstance();
			tmpCalendarTo.setTime(p_req7505.getDateTo());

			int tmpIntDateFrom = 0;
			int tmpIntDateTo = 0;
			
			ActSetItem actItem = getBankConfig().getActSetItem(p_req7505.actNum, "");
			if (actItem == null) {
				retAts.writeRespCode("-1");
				retAts.writeRespInfo("未找到账号" + p_req7505.actNum + "对应的商户配置信息");
				retAts.writeEnd();
				return retAts;
			}
			
			// 按自然日循环查询
			while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
				AllInPay02Header bankHeader = new AllInPay02Header();
				bankHeader.REQ_SN = getQryTransID();
				bankHeader.TRX_CODE = "300001";

				XmlTextWriter xmlWriter = new XmlTextWriter();
				writeAgrmCommonHeader(xmlWriter, bankHeader, actItem);
				xmlWriter.writeStartElement("AHQUERYREQ");
				xmlWriter.writeElementString("ACCTNO", p_req7505.actNum);
				xmlWriter.writeElementString("STARTDAY", Format.Date8Format.format(tmpCalendarFrom.getTime()));
				xmlWriter.writeElementString("ENDDAY", Format.Date8Format.format(tmpCalendarFrom.getTime()));
				xmlWriter.writeEndDocument();

				String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), actItem);
				if (tmpStrRet[0].length() == 0) {
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
					
					if (tmpBkReader != null) {
						String tmpRetCode = tmpBkReader.getFirstText("/AIPG/INFO/RET_CODE");
						if ("000000".equals(tmpRetCode)) {
							if (tmpBkReader.read("/AIPG/AHQUERYREP/BALNODE")) {
								RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
								
								String tmpETYDAT = tmpBkReader.readedXElement.getSingleText("PERIODDAY");
								tmpRetDetail.balDate = tmpETYDAT.substring(0, 4) + "-" + tmpETYDAT.substring(4, 6) + "-" + tmpETYDAT.substring(6, 8);
								tmpRetDetail.actBal = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("CLOSINGBAL"));
								retAts.writeRetDetail(tmpRetDetail);
							}
						} else {
							tmpRespCode = "-1";
							tmpRespInfo = tmpBkReader.getFirstText("/AIPG/INFO/ERR_MSG");
						}
					} else {
						tmpRespCode = "-1";
						tmpRespInfo = tmpSbMsg.toString();
					}
				} else {
					tmpRespCode = "-1";
					tmpRespInfo = tmpStrRet[1];
					break;
				}
				
				tmpCalendarFrom.add(Calendar.DATE, 1);
				tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
				tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRespCode = "-1";
			tmpRespInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		if ("-1".equals(tmpRespCode)) {
			retAts.writeRespCode(tmpRespCode);
			retAts.writeRespInfo(tmpRespInfo);
		} else if (retAts.getWrittenRdCount() == 0)	{
			retAts.writeRespCode("0");
			retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
		}
		
		retAts.writeEnd();
		return retAts;
	}
}
